// var items = require('data.js');
const App = getApp()
import Request from '../../../utils/request.js'
let app = getApp()
let store = app.store

const items = [{
  addressId: '1',
  address: '详细地址',
  addressZone: ['广东省', '广州市', '海珠区'],
  contactName: "祁同伟",
  contactMobile: '18902179373',
  isDefault: 0
}, {
  addressId: '2',
  address: '详细地址',
  addressZone: ['广东省', '广州市', '海珠区'],
  contactName: "祁同伟",
  contactMobile: '18902179373',
  isDefault: false
}]

Page({
  data: {
    items: [],
    curIdx: 0,
    model: "default",
    selectedAddress: null,
  },
  onLoad: function(options) {
    if (options.model) {
      this.setData({
        model: options.model
      });
    }

    options.selectedAddress && this.setData({
      selectedAddress: JSON.parse(options.selectedAddress)
    })
  },
  onShow() {
    this.getList();
  },

  //获取就诊人列表
  getList: function() {
    wx.showLoading({
      icon: "loading"
    });
    //获取就诊人列表
    let data = Request.postRequest('/patientAddress/getAddress', {
      status: '1'
    }).then(data => {
      let idx = 0;
      for (let i = 0; i < data.data.length; i++) {
        let item = data.data[i];
        if (this.data.model === 'select' && this.data.selectedAddress) {
          if (item.addressId === this.data.selectedAddress.addressId) {
            idx = i;
            break;
          }
        } else {
          if (item.isDefault == 'true') {
            idx = i;
            break;
          }
        }
      }
      this.setData({
        curIdx: idx,
        items: data.data
      })
      wx.hideLoading()
    });
  },

  //添加就诊人
  addPerson: function(event) {
    wx.navigateTo({
      url: '/pages/me/address/edit/edit'
    })
  },

  //点击跳转
  goDetails: function(event) {
    if (this.data.model != "default") {
      return;
    }
    let id = event.currentTarget.dataset.id;
    console.log(id);
    let items = this.data.items;
    let itemData = items.filter((item) => {
      return item.addressId === id;
    });
    console.log(itemData);
    wx.navigateTo({
      url: '/pages/me/address/edit/edit?data=' + JSON.stringify(itemData[0])
    })
  },

  //手指触摸动作开始 记录起点X坐标
  touchstart: function(e) {
    //开始触摸时 重置所有删除
    let data = App.touch._touchstart(e, this.data.items)
    this.setData({
      items: data
    })
  },

  //滑动事件处理
  touchmove: function(e) {
    let data = App.touch._touchmove(e, this.data.items)
    this.setData({
      items: data
    })
  },
  deleteRequest(id) {
    if (!id) return;
    let item = this.data.items.filter((item) => {
      return item.addressId === id
    })
    console.log(item[0]);
    let self = this;
    let data = Request.postRequest('/patientAddress/delete', {
      patientId: store.getState().systemLoginData.userId || wx.getStorageSync('userId'),
      addressId: item[0].addressId
    }).then(data => {
      self.setData({
        items: data.data
      })

      self.getList();
    })
  },

  //删除事件
  del: function(e) {
    let id = e.currentTarget.dataset.id;
    console.log(id);
    let self = this;
    wx.showModal({
      title: '提示',
      content: '确认要删除配送地址吗？',
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          // .data.items.splice(e.currentTarget.dataset.index, 1)
          // .setData({
          //   items: .data.items
          // })

          self.deleteRequest(id);

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  selItem(evt) {
    let idx = evt.currentTarget.dataset.index;
    this.setData({
      curIdx: idx
    });
  },
  confirmAndGoback() {
    wx.navigateBack();
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    prevPage.setData({
      addressInfo: this.data.items[this.data.curIdx] || {}
    });
  }
})